package com.example.celldian.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBBonus extends SQLiteOpenHelper {

	public DBBonus(Context activity) {
		super(activity, "Celldian.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String query = "CREATE TABLE bonus ( bonusname TEXT PRIMARY KEY, status TEXT, bonusdagame TEXT, type TEXT)";
		
		db.execSQL(query);	
		
		Log.i("DATABASE DBBonus:","TABLE bonus SUCCESSFULY CREATED!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String query = "DROP TABLE IF EXISTS bonus";
		
		db.execSQL(query);
		onCreate(db);				
		
		Log.i("DATABASE DBBonus:","UPDATE SUCCESSFUL!");
	}
	
	public void insertBonus(HashMap<String ,String> bonusValues) {
			
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("bonusname", bonusValues.get("bonusname"));
		values.put("status", bonusValues.get("status"));
		values.put("bonusdagame", bonusValues.get("bonusdagame"));
		values.put("type", bonusValues.get("type"));
		
		db.insert("bonus", null, values);
		db.close();
		
		Log.i("DATABASE DBBonus:","BONUS INSERT WAS SUCCESSFUL!");
	}
	
	public int updateBonus(HashMap<String ,String> bonusValues) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("bonusname", bonusValues.get("bonusname"));
		values.put("status", bonusValues.get("status"));
		values.put("bonusdagame", bonusValues.get("bonusdagame"));
		values.put("type", bonusValues.get("type"));
		
		Log.i("DATABASE DBBonus:","INVENTORY VALUE UPDATE WAS SUCCESSFUL!");
		
		return db.update("bonus", values, "bonusname" + "?=", new String[] {bonusValues.get("bonusname")});
	}
	
	public void deleteBonus(String bonusname) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		String query = "DELETE FROM bonus WHERE bonusname='" + bonusname + "'";
		
		db.execSQL(query);
		
		Log.i("DATABASE DBBonus:","BONUS DELETE WAS SUCCESSFUL!");
	}
	
	public ArrayList<HashMap<String, String>> getBonusList() {
		
		ArrayList<HashMap<String, String>> bonusList = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM bonus";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				HashMap<String, String> bonus = new HashMap<String, String>();
				
				bonus.put("bonusname", cursor.getColumnName(0));
				bonus.put("status", cursor.getColumnName(1));
				bonus.put("bonusdagame", cursor.getColumnName(2));
				bonus.put("type", cursor.getColumnName(3));
				
				bonusList.add(bonus);
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBBonus:","BONUS RETURN WAS SUCCESSFUL!");
		
		return bonusList;
	}
	
	public HashMap<String, String> getBonus(String bonusname){
		
		HashMap<String, String> bonus = new HashMap<String, String>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM bonus WHERE bonusname='" + bonusname + "'";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				bonus.put("bonusname", cursor.getColumnName(0));
				bonus.put("status", cursor.getColumnName(1));
				bonus.put("bonusdagame", cursor.getColumnName(2));
				bonus.put("type", cursor.getColumnName(3));
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBBonus:","BONUS RETURN WAS SUCCESSFUL!");
		
		return bonus;
	}
}
